/* @(#)FmsContext.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fms.miscellaneous;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.flywheelms.library.fmm.FmmDatabaseMediator;
import com.flywheelms.library.fmm.node.FmmNodeInfo;
import com.flywheelms.library.fmm.perspective.FmmPerspective;
import com.flywheelms.library.fmm.serialization.FmmSerialization;
import com.flywheelms.library.fse.model.FseDocumentSectionTribKn;

public class FmsContext implements FmmSerialization {
	
	private static final String key__PERSPECTIVE_NAME = "PerspectiveName";
	private static final String key__NODE_ID_STRING_LIST = "NodeList";
	private FmmPerspective perspective;
	private final ArrayList<FmmNodeInfo> nodeList = new ArrayList<FmmNodeInfo>();
	
	public FmsContext(FmmPerspective aPerspective) {
		this.perspective = aPerspective;
	}
	
	public FmsContext(JSONObject aJsonObject) {
		try {
			this.perspective = FmmPerspective.getObjectForName(aJsonObject.getString(key__PERSPECTIVE_NAME));
			JSONArray theJsonArray = aJsonObject.getJSONArray(key__NODE_ID_STRING_LIST);
			for(int i=0;i < theJsonArray.length();i++){                                     
				String theNodeIdString = theJsonArray.getString(i);
				this.nodeList.add(FmmDatabaseMediator.getActiveMediator().getFmmNodeSummary(theNodeIdString));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}         
	}

	public FmmPerspective getPerspective() {
		return this.perspective;
	}

	public ArrayList<FmmNodeInfo> getNodeList() {
		return this.nodeList;
	}
	
	public void addContext(FmmNodeInfo anFmmNodeInfo) {
		this.nodeList.add(anFmmNodeInfo);
	}
	
	public int getPerspectiveIconDrawableResourceId() {
		return this.perspective.getIconDrawableResourceId();
	}

	@Override
	public String getSerialized() {
		return getJsonObject().toString();
	}
	
	@Override
	public JSONObject getJsonObject() {
		JSONObject theJsonObject = new JSONObject();
		try {
			theJsonObject.put(key__PERSPECTIVE_NAME, this.perspective.getName());
			JSONArray theJsonArray = new JSONArray();
			for (FmmNodeInfo theFmmNodeInfo : this.nodeList) {
				theJsonArray.put(theFmmNodeInfo.getNodeIdString());
			}
			theJsonObject.put(key__NODE_ID_STRING_LIST, theJsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return theJsonObject;
	}

	public FmsContext getClone() {
		return new FmsContext(getJsonObject());
	}
	
}
